'use strict';



module.exports = {
	/**
	 * 获取收支报告
	 * @url client/accounting/record/kh/getStatements 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event, context) => {
		let { data = {}, userInfo, util, filterResponse, originalParam } = event;
		let { customUtil, uniID, config, pubFun, vk, db, _, $ } = util;
		let { uid, accountBook } = data;
		let res = { code: 0, msg: "查询成功" };
		// 业务逻辑开始-----------------------------------------------------------
		if(accountBook == null ){
			return { code : 1001, msg : '参数错误' };
		}
		
		let {_id} = accountBook;
		if(pubFun.isEmpty(_id)){
			return { code : 1001, msg : '参数错误' };
		}
		
		// 获取当日时间
		let statements = {
			today:{
				date: Date.now(),
				totalRevenue: 0,
				totalEcpenditure: 0
			},
			week:{
				startDate: pubFun.getWeekStartTimestamp(),
				endDate: pubFun.getWeekEndTimestamp(),
				totalRevenue: 0,
				totalEcpenditure: 0
			},
			month:{
				startDate: pubFun.getMonthStartTimestamp(),
				endDate: pubFun.getMonthEndTimestamp(),
				totalRevenue: 0,
				totalEcpenditure: 0
			}
		}
		
		let rtn = await db.collection('db-book-relation-data').aggregate()
			.match({
				bookId: _id,
			})
			.lookup({
				from: "db-book-keeping-data",
				localField: "dataId",
				foreignField: "_id",
				as: "dataInf"
			})
			.unwind('$dataInf') // 将数组展开为对象
			.addFields({
				// 合并副表字段到主表
				isExpenses: '$dataInf.isExpenses',
				amount: '$dataInf.amount',
				time: "$dataInf.time"
			})
			// 移除不显示字段
			.project({
				dataInf: 0
			})
			// 再次筛选附表中字段
			.match({
				time: _.gt(pubFun.getTodayStartTimestamp()).lt(pubFun.getTodayEndTimestamp()),
			})
			// 分组查询
			.group({
				_id: {
					isExpenses: '$isExpenses',
				},
				isExpenses: {
					$first: '$isExpenses'
				},
				amount: {
					$sum: '$amount'
				}
			})
			.end();
		
		if(pubFun.isNotEmpty(rtn.data)) {
			for (const item of rtn.data) {
				if(item.isExpenses === "true") {
					statements.today.totalEcpenditure = item.amount
				}
				else if (item.isExpenses === "false") {
					statements.today.totalRevenue = item.amount
				}
			}
		}
		
		let rtnWeek = await db.collection('db-book-relation-data').aggregate()
			.match({
				bookId: _id,
			})
			.lookup({
				from: "db-book-keeping-data",
				localField: "dataId",
				foreignField: "_id",
				as: "dataInf"
			})
			.unwind('$dataInf') // 将数组展开为对象
			.addFields({
				// 合并副表字段到主表
				isExpenses: '$dataInf.isExpenses',
				amount: '$dataInf.amount',
				time: "$dataInf.time"
			})
			// 移除不显示字段
			.project({
				dataInf: 0
			})
			// 再次筛选附表中字段
			.match({
				time: _.gt(pubFun.getWeekStartTimestamp()).lt(pubFun.getWeekEndTimestamp()),
			})
			// 分组查询
			.group({
				_id: {
					isExpenses: '$isExpenses',
				},
				isExpenses: {
					$first: '$isExpenses'
				},
				amount: {
					$sum: '$amount'
				}
			})
			.end();
		
		if(pubFun.isNotEmpty(rtnWeek.data)) {
			for (const item of rtnWeek.data) {
				if(item.isExpenses === "true") {
					statements.week.totalEcpenditure = item.amount
				}
				else if (item.isExpenses === "false") {
					statements.week.totalRevenue = item.amount
				}
			}
		}
		
		let rtnMonth = await db.collection('db-book-relation-data').aggregate()
			.match({
				bookId: _id,
			})
			.lookup({
				from: "db-book-keeping-data",
				localField: "dataId",
				foreignField: "_id",
				as: "dataInf"
			})
			.unwind('$dataInf') // 将数组展开为对象
			.addFields({
				// 合并副表字段到主表
				isExpenses: '$dataInf.isExpenses',
				amount: '$dataInf.amount',
				time: "$dataInf.time"
			})
			// 移除不显示字段
			.project({
				dataInf: 0
			})
			// 再次筛选附表中字段
			.match({
				time: _.gt(pubFun.getMonthStartTimestamp()).lt(pubFun.getMonthEndTimestamp()),
			})
			// 分组查询
			.group({
				_id: {
					isExpenses: '$isExpenses',
				},
				isExpenses: {
					$first: '$isExpenses'
				},
				amount: {
					$sum: '$amount'
				}
			})
			.end();
		
		if(pubFun.isNotEmpty(rtnMonth.data)) {
			for (const item of rtnMonth.data) {
				if(item.isExpenses === "true") {
					statements.month.totalEcpenditure = item.amount
				}
				else if (item.isExpenses === "false") {
					statements.month.totalRevenue = item.amount
				}
			}
		}
		
		res.statements = statements
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}